import { _decorator, animation, Component, Node } from 'cc';
import { eventTarget, GameEvent } from '../Events/EventBus';
const { ccclass, property } = _decorator;

@ccclass('PlayerAnimationController')
export class PlayerAnimationController extends Component {
    private playerAnimationController: animation.AnimationController | null = null;

    start() {
        // 获取到玩家动画节点的动画控制器
        this.playerAnimationController = this.node.getComponent(animation.AnimationController);
        // 注册全局事件：普通攻击事件
        eventTarget.on(GameEvent.PLAYER_NORMAL_ATTACK, this.playerAttackA, this);
        eventTarget.on(GameEvent.Player_Move, this.playerMove, this);
        eventTarget.on(GameEvent.Player_Idle, this.playerIdle, this);
    }
    onDestroy() {
        eventTarget.off(GameEvent.PLAYER_NORMAL_ATTACK, this.playerAttackA, this);
        eventTarget.off(GameEvent.Player_Move, this.playerMove, this);
        eventTarget.off(GameEvent.Player_Idle, this.playerIdle, this);
    }

    playerIdle(){
        // 这样在PlayerControl里面判断静止时，会派发静止事件，这边会监听
        // 且只有在攻击变量为false时才重置角色为idle状态
        // 避免原方案在PlayerControl的update里重置Idle为true，会和进行攻击时AttackA为true重复触发切换状态
        let isAttackA = this.playerAnimationController.getValue("AttackA");
        
        let isIdle = this.playerAnimationController.getValue("Idle");
        if(!isAttackA && !isIdle ){
            this.playerAnimationController.setValue("Walk", false);
            this.playerAnimationController.setValue("Idle", true);
        }
    }

    playerMove(walkDir: number){
        //通过移动方向walkDir判断移动方向
        // this.playerAnimationController.setValue("Idle", false);
        let isAttackA = this.playerAnimationController.getValue("AttackA");
        let isIdle = this.playerAnimationController.getValue("Idle");
        // console.log(isIdle);
        // console.log(isAttackA);
        if(!isAttackA){
            this.playerAnimationController.setValue("Walk", true);
            this.playerAnimationController.setValue("Idle", false);
            this.playerAnimationController.setValue("WalkDir", walkDir);
        }

        // if (walkDir == 1 &&  isAttackA == false) {         
        //     // this.playerAnimationController.setValue("Idle", false);
        //     this.playerAnimationController.setValue("WalkDir", walkDir);
        // } else if (walkDir == -1 && isAttackA == false) {
        //     // this.playerAnimationController.setValue("Idle", false);
        //     this.playerAnimationController.setValue("WalkDir", walkDir);
        // }
    }


    playerAttackA() {
        if (!this.playerAnimationController) return;
        let isAttackA = this.playerAnimationController.getValue("AttackA");

        // console.log("普通攻击事件！")
        // 通过设置参数触发动画图中的攻击状态
        if (!isAttackA) {
            // 当不处于攻击状态时才能攻击！
            this.playerAnimationController.setValue("AttackA", true);

            // 因为触发了攻击，所以要把Idle改为false
            // this.playerAnimationController.setValue("Idle", false);
            
        }


        // const attackA = this.playerAnimationController.getValue('AttackA');
        // console.log("普通攻击变量AttackA的值为：" + attackA);

        if (isAttackA) {
            // 添加动画完成回调，
            this.scheduleOnce(() => {
                // 攻击动画结束以后再将Idle改为true
                // this.playerAnimationController.setValue("Idle", true);
                this.playerAnimationController!.setValue('AttackA', false);
            }, 0.18);  // 根据动画时长调整
        }

    }
    

    update(deltaTime: number) {

    }
}


